package com.liukaixin.product.navi;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by liukaixin on 16/10/12.
 */
public class MapActivity extends AppCompatActivity {


    private Handler handler = new Handler();

    private ImageView mapImg;

    private String entrance = "A";
    private String emptyPosition = "3-1";

    private View line1;
    private View line2;
    private View line19;
    private View line22;
    private View line3;
    private View line4;
    private View line36;
    private View line38;
    private View line42;
    private View line53;

    private View line55;
    private View line58;
    private View line60;
    private View line104;
    private View line107;
    private View line110;
    private View line111;
    private View line121;
    private View line123;

    private View line33;
    private View line30;
    private View line27;
    private View line24;
    private View line21;

    private View line35;
    private View line50;
    private View line47;
    private View line45;
    private View line41;

    private View line52;
    private View line67;
    private View line64;
    private View line61;
    private View line44;

    private View line6;
    private View line5;

    private View line103;
    private View line102;
    private View line100;
    private View line98;
    private View line95;
    private View line92;
    private View line89;

    private View line120;
    private View line119;
    private View line116;
    private View line113;

    private View line134;
    private View line132;
    private View line130;
    private View line127;
    private View line124;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        initViews();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        mapImg = (ImageView) findViewById(R.id.map_img);

        if (AppController.getInstance().getCache().getAsString("emptyPosition") != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("您的车已停到[" + emptyPosition + "]号车位");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            builder.setIcon(R.drawable.logo);
            builder.show();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请选择入口");
            builder.setSingleChoiceItems(new String[]{"A", "B", "C", "D", "E", "F"}, 0,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case 0:
                                    entrance = "A";
                                    break;
                                case 1:
                                    entrance = "B";
                                    break;
                                case 2:
                                    entrance = "C";
                                    break;
                                case 3:
                                    entrance = "D";
                                    break;
                                case 4:
                                    entrance = "E";
                                    break;
                                case 5:
                                    entrance = "F";
                                    break;
                            }
                        }
                    });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MapActivity.this, "请稍后,为您查找空位中……", Toast.LENGTH_SHORT).show();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            askToChoosePosition();
                        }
                    }, 3000);
                }
            });
            builder.setIcon(R.drawable.logo);
            builder.show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_finish_park) {
            AppController.getInstance().getCache().put("emptyPosition", emptyPosition);
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initViews() {
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line19 = findViewById(R.id.line19);
        line22 = findViewById(R.id.line22);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);
        line36 = findViewById(R.id.line36);
        line38 = findViewById(R.id.line38);
        line42 = findViewById(R.id.line42);
        line53 = findViewById(R.id.line53);
        line55 = findViewById(R.id.line55);
        line58 = findViewById(R.id.line58);
        line60 = findViewById(R.id.line60);
        line104 = findViewById(R.id.line104);
        line107 = findViewById(R.id.line107);
        line110 = findViewById(R.id.line110);
        line111 = findViewById(R.id.line111);
        line121 = findViewById(R.id.line121);
        line123 = findViewById(R.id.line123);
        line33 = findViewById(R.id.line33);
        line30 = findViewById(R.id.line30);
        line27 = findViewById(R.id.line27);
        line24 = findViewById(R.id.line24);
        line21 = findViewById(R.id.line21);
        line35 = findViewById(R.id.line35);
        line50 = findViewById(R.id.line50);
        line47 = findViewById(R.id.line47);
        line45 = findViewById(R.id.line45);
        line41 = findViewById(R.id.line41);
        line52 = findViewById(R.id.line52);
        line67 = findViewById(R.id.line67);
        line64 = findViewById(R.id.line64);
        line61 = findViewById(R.id.line61);
        line44 = findViewById(R.id.line44);
        line6 = findViewById(R.id.line6);
        line5 = findViewById(R.id.line5);
        line103 = findViewById(R.id.line103);
        line102 = findViewById(R.id.line102);
        line100 = findViewById(R.id.line100);
        line98 = findViewById(R.id.line98);
        line95 = findViewById(R.id.line95);
        line92 = findViewById(R.id.line92);
        line89 = findViewById(R.id.line89);
        line120 = findViewById(R.id.line120);
        line119 = findViewById(R.id.line119);
        line116 = findViewById(R.id.line116);
        line113 = findViewById(R.id.line113);
        line134 = findViewById(R.id.line134);
        line132 = findViewById(R.id.line132);
        line130 = findViewById(R.id.line130);
        line127 = findViewById(R.id.line127);
        line124 = findViewById(R.id.line124);
    }

    private void askToChoosePosition() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择空位");
        builder.setSingleChoiceItems(new String[]{"3-1", "5-2", "6-3", "12-3", "15-1"}, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                emptyPosition = "3-1";
                                break;
                            case 1:
                                emptyPosition = "5-2";
                                break;
                            case 2:
                                emptyPosition = "6-3";
                                break;
                            case 3:
                                emptyPosition = "12-3";
                                break;
                            case 4:
                                emptyPosition = "15-1";
                                break;
                        }
                    }
                });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MapActivity.this, "请稍后, 生成路径中……", Toast.LENGTH_SHORT).show();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<View> pathList = getPath(entrance, emptyPosition);
                        showPath(pathList);
                    }
                }, 3000);
            }
        });
        builder.setIcon(R.drawable.logo);
        builder.show();
    }

    private List<View> getPath(String entrance, String emptyPosition) {
        if (entrance.equals("A") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (entrance.equals("A") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (entrance.equals("A") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line4);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (entrance.equals("A") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (entrance.equals("A") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line4);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (entrance.equals("B") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line33);
            list.add(line30);
            list.add(line27);
            list.add(line24);
            list.add(line21);
            list.add(line22);
            return list;
        }
        if (entrance.equals("B") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line50);
            list.add(line47);
            list.add(line45);
            list.add(line41);
            list.add(line42);
            return list;
        }
        if (entrance.equals("B") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line52);
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line60);
            return list;
        }
        if (entrance.equals("B") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line38);
            list.add(line36);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (entrance.equals("B") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line52);
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line58);
            list.add(line55);
            list.add(line53);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (entrance.equals("C") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line35);
            list.add(line33);
            list.add(line30);
            list.add(line27);
            list.add(line24);
            list.add(line21);
            list.add(line22);
            return list;
        }
        if (entrance.equals("C") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line42);
            return list;
        }
        if (entrance.equals("C") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line60);
            return list;
        }
        if (entrance.equals("C") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line38);
            list.add(line36);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (entrance.equals("C") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line58);
            list.add(line55);
            list.add(line53);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (entrance.equals("D") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line3);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (entrance.equals("D") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (entrance.equals("D") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (entrance.equals("D") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (entrance.equals("D") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (entrance.equals("E") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line102);
            list.add(line100);
            list.add(line98);
            list.add(line95);
            list.add(line92);
            list.add(line89);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (entrance.equals("E") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line120);
            list.add(line119);
            list.add(line116);
            list.add(line113);
            list.add(line110);
            list.add(line107);
            list.add(line104);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (entrance.equals("E") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line121);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (entrance.equals("E") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line120);
            list.add(line119);
            list.add(line113);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (entrance.equals("E") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line123);
            return list;
        }
        if (entrance.equals("F") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line102);
            list.add(line100);
            list.add(line98);
            list.add(line95);
            list.add(line92);
            list.add(line89);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (entrance.equals("F") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line119);
            list.add(line113);
            list.add(line110);
            list.add(line107);
            list.add(line104);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (entrance.equals("F") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line120);
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line121);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (entrance.equals("F") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line119);
            list.add(line116);
            list.add(line113);
            list.add(line111);
            return list;
        }
        if (entrance.equals("F") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line120);
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line123);
            return list;
        }
        return null;
    }

    private void showPath(List<View> pathList) {
        for (View view : pathList) {
            setVisible(view);
        }
    }

    private void setVisible(View view) {
        view.setVisibility(View.VISIBLE);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}
